package com.sooth.micro.page.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.sooth.common.core.domain.R;
import com.sooth.common.log.annotation.Log;
import com.sooth.common.web.core.BaseController;
import com.sooth.micro.page.service.IMicroPageService;
import com.sooth.micro.page.view.MicroAppPageVo;
import com.sooth.micro.page.view.MicroPageBo;
import com.sooth.micro.page.view.MicroPageDto;
import com.sooth.micro.page.view.MicroPageVo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 在线页面
 *
 * @author Lion Li
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/micro/page")
public class MicroPageController extends BaseController {

    private final IMicroPageService microPageService;

    /**
     * 获取在线页面列表
     */
    @SaCheckPermission("system:micro-page:query")
    @GetMapping("/list")
    public R<List<MicroPageVo>> list(MicroPageDto microAppDto) {
        return R.ok(microPageService.selectList(microAppDto));
    }


    /**
     * 根据在线页面编号获取详细信息
     *
     * @param id 在线页面ID
     */
    @SaCheckPermission("system:micro-page:query")
    @GetMapping(value = "/query")
    public R<MicroPageBo> query(@RequestParam Long id) {
        return R.ok(microPageService.selectById(id));
    }

    @SaCheckPermission("system:micro-page:query")
    @GetMapping(value = "/getAppId")
    public R<Long> getAppId(@RequestParam Long id) {
        return R.ok(microPageService.getAppId(id));
    }

    /**
     * 新增在线页面
     */
    @SaCheckPermission("system:micro-page:add")
    @Log(title = "在线页面管理", message = "新增在线页面")
    @PostMapping("/add")
    public R<Long> add(@Validated @RequestBody MicroPageBo microApp) {
        microPageService.insertFetchKey(microApp);
        return R.ok(microApp.getId());
    }

    /**
     * 修改在线页面
     */
    @SaCheckPermission("system:micro-page:edit")
    @Log(title = "在线页面管理", message = "修改在线页面")
    @PutMapping("/edit")
    public R<Void> edit(@Validated @RequestBody MicroPageBo microApp) {
        return toAjax(microPageService.updateById(microApp));
    }

    /**
     * 删除在线页面
     *
     * @param id 在线页面ID
     */
    @SaCheckPermission("system:micro-page:remove")
    @Log(title = "在线页面管理", message = "删除在线页面")
    @DeleteMapping("/remove")
    public R<Void> remove(@RequestParam Long id) {
        return toAjax(microPageService.deleteById(id));
    }

    /**
     * 保存在线应用页面
     *
     * @param appPageVo 角色部门
     * @return
     */
    @SaCheckPermission("system:micro-page:save")
    @Log(title = "在线应用管理", message = "保存应用页面")
    @PostMapping("/save")
    public R<Void> save(@RequestBody MicroAppPageVo appPageVo) {
        microPageService.save(appPageVo);
        return R.ok("操作成功");
    }
}
